package com.cssiot.webApi.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class LoginInterceptor implements HandlerInterceptor  {
	private static Logger logger = LoggerFactory
			.getLogger(LoginInterceptor.class);
	
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		
	}

	//登陆验证拦截器
	@Override
	@SuppressWarnings("unchecked")
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object obj) throws Exception {
//		请求路径
		String path=request.getServletPath();
		path=path.substring(path.lastIndexOf("/")+1, path.length());
//		项目路径
		 String contextPath = request.getContextPath();
		String appUrl="verifyLogin|updatePassWord|findOneMac|findOneMacPart|getTopData|getRegionMacData|getRegionDetailMacData|getRegionDetailMacUseLoc|getGasMacData|getGasDetailData|getMacChekData|verifyData|" +
				"getMacDetailChekData|getElecCheckMacData|gasQuantitySave|uploadMacPic|downLoadMacPartImg|updateUserPassWord|signMcsSave|findTBV|signRFIDSave|saveAppLogInfo|getGasQRCodeInfo|getTopMacInfoData|getRfidIsBound|getMcsIsBound|findEquipmentTypeInfo|findMcsAllInfo";
			/*if(path.endsWith("login.jsp")||path.endsWith("image.jsp")||path.endsWith("404.jsp")||path.endsWith("error.jsp")){
				return true;
			}else{
				request.getRequestDispatcher("/jsp/error/error.jsp").forward(request, response);
				return false;
			}*/
		
		if(appUrl.contains(path)){
			return true;
		}else{
//			if(path.endsWith(".woff")||path.endsWith(".ttf")){
//				return true;
//			}
			if(path.endsWith(".htm")){
				if(path.contains("tlogin.htm")||path.contains("checkImageValue.htm")||path.contains("login.htm")
						||path.contains("logOut.htm")||path.contains("error.htm")||path.contains("signUser.htm")
						||path.contains("checkPassWord.htm")||path.contains("signUserSave.htm")||path.contains("isExist.htm")||path.contains("isExistRegUser.htm")||path.contains("updatePassWord.htm")||path.contains("reloading.htm")
						
						){
					return true;
				}else{
				/*	Gzz01UserData user=(Gzz01UserData)request.getSession().getAttribute("sysuser");
					if(user==null){
						response.sendRedirect(contextPath+"/login/reloading.htm");
//						response.sendRedirect(contextPath+"/tlogin.htm");//跳转jsp过滤直接访问s
//						request.getRequestDispatcher(contextPath+"/login.jsp").forward(request, response);
						return false;
					}else {
						return true;
					}*/
				}
			}else{
				response.sendRedirect(contextPath+"/login/error.htm");
				return false;
			}

		}
		return false;
		}
	}
